import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export async function GET(request: Request) {
  try {
    const { searchParams } = new URL(request.url);
    const name = searchParams.get('name');

    if (!name) {
      return NextResponse.json(
        { error: '缺少玩家名称参数' },
        { status: 400 }
      );
    }

    const player = await prisma.player.findUnique({
      where: { name },
      include: { scores: true }
    });

    if (!player) {
      return NextResponse.json(
        { error: '未找到该玩家' },
        { status: 404 }
      );
    }

    return NextResponse.json({
      id: player.id,
      name: player.name,
      avatar: player.avatar,
      region: player.region,
      scores: player.scores,
      isAdmin: player.isAdmin,
      swordTier: player.swordTier,
      crystalTier: player.crystalTier,
      buhcTier: player.buhcTier,
      potionTier: player.potionTier,
      npotTier: player.npotTier,
      smpTier: player.smpTier,
      createdAt: player.createdAt
    });
  } catch (error) {
    console.error('Player search error:', error);
    return NextResponse.json(
      { error: '查询玩家失败' },
      { status: 500 }
    );
  }
} 